package com.hqq.leetcode.dp;

/**
 * LongestIncreasingContinuousSubsequence 最长上升连续子序列
 * Description:
 * Give an integer array，find the longest increasing continuous subsequence in this array.
 * <p>
 * An increasing continuous subsequence:
 * <p>
 * Can be from right to left or from left to right.
 * Indices of the integers in the subsequence should be continuous.
 * <p>
 * Input:
 * [5,4,2,1,3]
 * [5,1,2,3,4]
 * Output:
 * 4
 * 4
 * Created by heqianqian on 2017/9/14.
 */
public class LongestIncreasingContinuousSubsequence {

    public static void main(String[] args) {
        /*data preparation*/
        int[] array1 = new int[]{5, 4, 2, 1, 3};
        int[] array2 = new int[]{5, 1, 2, 3, 4};

        LongestIncreasingContinuousSubsequence lic = new LongestIncreasingContinuousSubsequence();
        int result = lic.longestIncreasingContinuousSubsequence(array1);
        System.out.println(result);
        result = lic.longestIncreasingContinuousSubsequence(array2);
        System.out.println(result);
    }

    /*
     * @param A: An array of Integer
     * @return: an integer
     */
    public int longestIncreasingContinuousSubsequence(int[] A) {
        // write your code here
        if (A == null || A.length == 0) {
            return 0;
        }
        int res = 1, cnt1 = 1, cnt2 = 1;
        //从左往右遍历 如果是递增cn1加一 如果是递减cnt2加一
        for (int i = 0; i < A.length - 1; i++) {
            if (A[i] < A[i + 1]) {//increase
                cnt1++;
                cnt2 = 1;
            } else {//decrease
                cnt2++;
                cnt1 = 1;
            }
            res = Math.max(res, Math.max(cnt1, cnt2));
        }
        return res;
    }
}
